package com.cn.xiongmeng.czlxz.ui;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cn.xiongmeng.czlxz.R;
import com.cn.xiongmeng.czlxz.activity.IndexActivity;

public class TasksFragment extends Fragment implements View.OnClickListener {
    private final String TAG = "TasksFragment";
    private IndexActivity activity;
    private TextView tvTask1;
    private TextView tvTask2;

    public TasksFragment() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Log.d(TAG, "onAttach");

        activity = (IndexActivity) context;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        // Log.d(TAG, "onCreateView");
        View rootView = inflater.inflate(R.layout.fragment_tasks, container, false);

        ImageView btnOff = rootView.findViewById(R.id.btn_off);
        tvTask1 = rootView.findViewById(R.id.tv_task1);
        tvTask2 = rootView.findViewById(R.id.tv_task2);

        btnOff.setOnClickListener(this);

        return rootView;
    }
    @Override
    public void onStart() {
        super.onStart();
        Log.d(TAG, "onStart");

        setTaskStyle();
    }
    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");

    }
    @Override
    public void onClick(View v){
        if (v.getId() == R.id.btn_off){
            Log.d(TAG, "点击叉号");

            removeFragment(this);
        }
    }
    private void setTaskStyle(){
        SharedPreferences prefs = activity.getSharedPreferences("AppPrefs", Context.MODE_PRIVATE);

        int task1Cur = prefs.getInt("task1Cur", 0);
        if (activity.curReward == 0 &&  task1Cur != 0){
            task1Cur = 0;
            SharedPreferences.Editor editor = prefs.edit();
            editor.putInt("task1Cur", 0);
            editor.apply();
        }

        int completeTarget = prefs.getInt("completeTarget", 0);

        if (completeTarget == 0){
            // 任务一，（测试完成）
            if (task1Cur > 8){
                task1Cur = 8;
            }
            String task1Text = "纠错先锋：闯关 " + task1Cur + "/8";
            tvTask1.setText(task1Text);

            // 任务二，（测试完成）
            int task2Cur = activity.curReward;
            int task2Target = activity.curTargetReward;
            if (task2Cur > task2Target){
                task2Cur = task2Target;
            }
            String task2Text = "纠错达人：看视频 " + task2Cur + "/" + task2Target;
            tvTask2.setText(task2Text);
        } else if (completeTarget == 1) {
            // 样式变更
            // 任务一，（测试完成）
            // int task1Cur = prefs.getInt("task1Cur", 0);
            if (task1Cur > 8){
                String task1Text = "纠错先锋：已完成";
                tvTask1.setText(task1Text);
            } else {
                String task1Text = "纠错先锋：未完成";
                tvTask1.setText(task1Text);
            }

            // 任务二，（测试完成）
            String completeTargetTime = prefs.getString("completeTargetTime", "");
            String task2Text = "纠错达人：已完成 " + completeTargetTime;
            tvTask2.setText(task2Text);
        }

    }
    private void removeFragment(Fragment fragment){
        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        if (fragment.isAdded()) {
            fragmentTransaction.remove(fragment);
        }
        fragmentTransaction.commit();
    }
}